#!/bin/sh

# version $Id: start-stop-status 99 2009-12-30 13:44:53Z fr10848 $

PHP_DIR="/usr/syno/etc"
PIDFILE="/var/run/httpd-user.pid"
SYNOPKG_PKGDEST="__SYNOPKGPKGDEST__"
PKG_SRC="/var/packages/$SYNOPKG_PKGNAME"
DESKTOP="/usr/syno/synoman/webman/3rdparty/$SYNOPKG_PKGNAME"

case $1 in
	start)
	
		# Include virtual host Configuration
		echo "Include ${SYNOPKG_PKGDEST}/conf/httpd-synoneuf.conf-user" >> $PHP_DIR/httpd-vhost.conf-user
		# let time to "echo" to work before restart apache-user
		sleep 4

		# restart apache
		/usr/syno/etc/rc.d/S97apache-user.sh restart
	;;
	stop)
		# remove virtual host configuration
		sed '/^Include/ s/.*httpd-synoneuf\.conf-user.*//g' $PHP_DIR/httpd-vhost.conf-user | sed '/^$/d' > $PHP_DIR/httpd.tmp && mv -f $PHP_DIR/httpd.tmp $PHP_DIR/httpd-vhost.conf-user
		# let time to "sed" to work before restart apache-user
		sleep 4

		# restart apache
		/usr/syno/etc/rc.d/S97apache-user.sh restart
	;;
	status)
		# check for pidfile
		if [ -f $PIDFILE ] ; then
		  PID=`cat $PIDFILE`
		  WEB=`cat /usr/syno/etc/httpd-vhost.conf-user | grep synoneuf`
		  if [ "x$WEB" != "x" ] && [ "x$PID" != "x" ] && kill -0 $PID 2>/dev/null ; then
			STATUS="Synoneuf running"
			exit 0
		  else
			STATUS="Synoneuf not running"
			exit 1
		  fi
		else
		  STATUS="WebServer not running"
		  exit 1
		fi
		;;
	log)
		exit 0
	;;
esac
